"""
des: qss加载
author: mr52hz
date: 2021-08-23
"""
import os


class QssReader(object):

    def __init__(self, qss_path=None):
        if os.path.exists(qss_path) and qss_path.endswith('qss'):
            self.qss_path = qss_path
        else:
            self.qss_path = None

    def loadCss(self, obj):
        """
        obj 要有setStyleSheet方法
        :param obj: <QWidget>
        :return: None
        """
        if self.qss_path is None:
            return

        if not hasattr(obj, 'setStyleSheet'):
            return
        with open(self.qss_path, 'r', encoding='utf-8') as f:
            obj.setStyleSheet(f.read())

    def changeQss(self, qss_path, obj):
        if qss_path is not None:
            if os.path.exists(qss_path) and qss_path.endswith('qss'):
                self.qss_path = qss_path
                self.loadCss(obj)
